package io.github.talelin.latticy.controller.v1.ag;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.dto.ag.building.CreateOrUpdateBuildingDTO;
import io.github.talelin.latticy.model.ag.BuildingDO;
import io.github.talelin.latticy.service.ag.BuildingService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/v1/ag/building")
@Validated
public class BuildingController {
    @Autowired
    private BuildingService buildingService;
    @GetMapping("/{id}")
    public BuildingDO getBuilding(@PathVariable(value = "id") @Positive(message = "id必须为正整数") int id){
        BuildingDO building = buildingService.getById(id);
        if (building ==null){
            throw new NotFoundException("未找到楼栋");
        }
        return building;
    }

    @GetMapping("/list")
    public List<BuildingDO> getBuildings(){
        List<BuildingDO> buildings = buildingService.getBuildings();
        if (buildings ==null){
            throw new NotFoundException("未找到楼栋");
        }
        return buildings;
    }

//    @GetMapping("/name")
//    public List<BuildingDO> getBuildingName(){
//        List<BuildingDO> buildings = buildingService.getBuildingName();
//        if (buildings ==null){
//            throw new NotFoundException("未找到楼栋信息");
//        }
//        return buildings;
//    }

   @PostMapping("/create")
    public CreatedVO createdBuilding(@RequestBody @Validated CreateOrUpdateBuildingDTO validator){
        buildingService.createBuilding(validator);
        return new CreatedVO("创建成功");
   }

   @DeleteMapping("/{id}")
   @PermissionMeta(value = "删除楼栋", module = "楼栋", mount = true)
   @GroupRequired
    public DeletedVO deletedBuilding(@PathVariable("id") @Positive(message = "{id}") int id){
        BuildingDO building = buildingService.getById(id);
        if (building == null){
            throw new NotFoundException("building not found",10022);
        }
        buildingService.deleteById(building.getId());
        return new DeletedVO(14);
   }

   @PutMapping("/{id}")
    public UpdatedVO updatedBuilding(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                                     @RequestBody @Validated CreateOrUpdateBuildingDTO validator){
        BuildingDO building = buildingService.getById(id);
       if (building == null){
           throw new NotFoundException("building not found",10022);
       }
       buildingService.updateBuilding(building,validator);
       return new UpdatedVO("更新成功");
   }
}
